package com.alisoft.couchdbclient

import java.util.*
import org.junit.Assert
import org.junit.*

public class CouchDBViewTest {
	private CouchDBClient client = new CouchDBClient("10.2.225.129", 5984);
	private static final DB_NAME = "view_test";
	@Before
	void init() {
		if (client.getDatabaseInfo(DB_NAME) != null) {
			client.deleteDatabase(DB_NAME);
		}
		client.createDatabase(DB_NAME);
	}
	
	@After
	void clear() {
		client.deleteDatabase(DB_NAME);
	}
	@Test
	void testCreateView() {
		String viewStr = """{
			"_id":"_design/view_test",
			"language": "javascript",
			"views": {
				"all": {
					"map": "function(doc) { emit(null, doc) }"
				}
			}
		}""";
		client.createDocument(DB_NAME, '_design/view_test', viewStr);
		
		def response = client.query(DB_NAME, "view_test", "all", null);
		println response;
		Assert.assertEquals(0, response.total_rows);
	}
}